angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'myConstant', 'dateFilter', 'DateCalculateService', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                      CacheService, myConstant, dateFilter, DateCalculateService) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };

            //查询条件
            vm.queryBean = {};
            vm.queryBean.endTime = DateCalculateService.getToday();
            vm.queryBean.startTime = DateCalculateService.getYesterday();

            /*------------------以上配置通用---------------------*/
            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;
            vm.checkEndDay = checkEndDay;
            vm.checkStartDay = checkStartDay;
            vm.totalCount = {};

            /*------------------以上方法名可选择性通用---------------------*/

            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };

            function queryDetail() {
                if (isNull(vm.queryBean.startTime) || isNull(vm.queryBean.endTime)) {
                    alert("请设置查询时间");
                    return;
                }
                var queryForm = document.getElementById('queryForm');
                vm.url = queryForm.getAttribute('action');
                //组织参数：查询条件 + 分页数据

                vm.pagination.list = null;
                vm.pagination.queryBean = {};
                vm.pagination.queryBean.startTime = dateFilter(vm.queryBean.startTime, 'yyyyMMdd');
                vm.pagination.queryBean.endTime = dateFilter(vm.queryBean.endTime, 'yyyyMMdd');
                vm.pagination.queryBean.bankName = vm.queryBean.bankName;
                $http.post(vm.url, vm.pagination, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                            /*                            alert(vm.pagination.list.length);
                             console.log(JSON.stringify(vm.pagination.list));*/
                            var applyTotal = 0;
                            var applySuc = 0;
                            var applyFail = 0;
                            var boundTotal = 0;
                            var boundSuc = 0;
                            var boundFail = 0;
                            for (var i = 0; i < vm.pagination.list.length; i++) {
                                applyTotal += vm.pagination.list[i].applyTotal;
                                applySuc += vm.pagination.list[i].applySuccessTotal;
                                applyFail += vm.pagination.list[i].applyFailTotal;
                                boundTotal += vm.pagination.list[i].boundTotal;
                                boundSuc += vm.pagination.list[i].boundSuccessTotal;
                                boundFail += vm.pagination.list[i].boundFailTotal;
                            }
                            vm.totalCount.applyTotal = applyTotal;
                            vm.totalCount.applySuc = applySuc;
                            vm.totalCount.applyFail = applyFail;
                            if(applyTotal==0){
                                vm.totalCount.applySucRate =division(0,1);
                            }else{
                                vm.totalCount.applySucRate = division(applySuc, applyTotal);
                            }
                            if(applyTotal==0){
                                vm.totalCount.applyFailRate =division(0,1);
                            }else{
                                vm.totalCount.applyFailRate = division(applyFail, applyTotal);
                            }
                            vm.totalCount.boundTotal = boundTotal;
                            vm.totalCount.boundSuc = boundSuc;
                            vm.totalCount.boundFail = boundFail;
                            if(boundTotal==0){
                                vm.totalCount.boundSucRate =division(0,1);
                            }else{
                                vm.totalCount.boundSucRate =  division(boundSuc, boundTotal);
                            }
                            if(boundTotal==0){
                                vm.totalCount.boundFailRate =division(0,1);
                            }else{
                                vm.totalCount.boundFailRate = division(boundFail, boundTotal);
                            }
                        } else {
                            alert(data.msg);
                        }
                    });

            };

            function isNull(data){
                return (data == "" || data == undefined || data == null);
            }

            function division(a, b) {
                return (a * 100 / b).toFixed(2)+"%";
            }

            function checkEndDay(startDate, endDate) {
                if (!isNull(startDate) && !isNull(endDate)) {
                    var startD = new Date(startDate);
                    var endD = new Date(endDate);
                    if (endD < startD) {
                        alert("日期选择错误！请重新选择!");
                        vm.queryBean.startTime = null;
                        vm.queryBean.endTime = DateCalculateService.getToday();
                        return false;
                    }
                    var days = parseInt((endD.getTime() - startD.getTime()) / (1000 * 60 * 60 * 24));
                    if (days > 92) {
                        alert("日期范围应在92天之内");
                        vm.queryBean.endTime = DateCalculateService.getToday();
                        return false;
                    }
                    return true;
                }
            }

            function checkStartDay(startDate, endDate) {
                if (!isNull(startDate) && !isNull(endDate)) {
                    var startD = new Date(startDate);
                    var endD = new Date(endDate);
                    if (startD > endD) {
                        alert("日期选择错误！请重新选择!");
                        vm.queryBean.startTime = null;
                        vm.queryBean.endTime = DateCalculateService.getToday();
                        return false;
                    }
                }
                return true;
            }

            function resetForm() {
                vm.queryBean = {};
            }


            //缓存数据初始化(需要缓存的key请自定义)
            vm.cached = {
                BANK_INFO: {},
                T_BANK_CODE: {},
                COMANY_CODE: {},
                BANK_CARD_TYPE: {},
                OPCIF_AGREEMENT_TYPE: {}
            };
            CacheService.initCache(vm.cached);


        }]);
